package com.shenyun.controller;

import com.shenyun.pojo.Member;
import com.shenyun.pojo.Student;
import com.shenyun.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/student")
public class StudentController {

    @Autowired
    StudentService service;

    //查询全部学生数据
    @RequestMapping("/StudentAll")
    public ModelAndView GetStudentsAll(){
        ModelAndView mv = new ModelAndView();
        List<Student> studentlist = service.GetStudentsAll();
        mv.addObject("studentlist",studentlist);
        mv.setViewName("students/showStudent");
        return mv;
    }

    //添加学生页面
    @RequestMapping("/addStudent")
    public ModelAndView AddStudent(){
        return new ModelAndView("students/addStudent");
    }

    //查询学生信息 学号
    @GetMapping("/StudentById")
    public ModelAndView GetStudentById(@RequestParam("number") Integer number){
        ModelAndView mv = new ModelAndView();
        List<Student> studentlist = service.GetStudentById(number);
        mv.addObject("studentlist",studentlist);
        mv.setViewName("students/showStudent");
        return mv;
    }

    //更新学生信息 学号
    @GetMapping("/UpdateStudent")
    public ModelAndView UpdateStudent(@RequestParam("number") Integer number){
        ModelAndView mv = new ModelAndView();
        List<Student> students = service.GetStudentById(number);
        mv.addObject("students", students.get(0));
        mv.setViewName("students/updateStudent");
        return mv;
    }

    //删除学生信息 学号
    @GetMapping("/deleteStudent")
    public ModelAndView deleteStudent(@RequestParam("number") Integer number) {
        service.deleteStudent(number);
        return new ModelAndView("redirect:/student/StudentAll");
    }

    //查询学生成绩 学号
    @RequestMapping("/queryScore")
    public ModelAndView queryScore(){
        return new ModelAndView("students/queryScoreByNmu");
    }

    //查询成绩页面
    @RequestMapping("/showScoreByGradeAndClbum")
    public ModelAndView showScoreByGradeAndClbum(){
        return new ModelAndView("students/showScoreByGradeAndClbum");
    }

    //查询成绩页面 年级+班级
    @RequestMapping("/showScoreByGradeServlet")
    public ModelAndView showScoreByGradeServlet(@RequestParam("clbum") String clbum,@RequestParam("grade") String grade){
        ModelAndView mv = new ModelAndView();
        List<Member> studentMembers = service.GetMembersStudent(clbum,grade);
        mv.addObject("studentlist",studentMembers);
        mv.setViewName("students/showScoreByGradeAndClbum");
        return mv;
    }

    //查询学生成绩页面
    @GetMapping("/queryScoreByNmu")
    public ModelAndView queryScoreByNmu(){
        return new ModelAndView("students/queryScoreByNmu");
    }

    //查询学生成绩 学号
    @GetMapping("/queryScoreByNmuServlet")
    public ModelAndView queryScoreByNmuServlet(@RequestParam("number") Integer number){
        ModelAndView mv = new ModelAndView();
        Member studentMember = service.GetStudentNumber(number);
        mv.addObject("studentlist",studentMember);
        mv.setViewName("students/queryScoreByNmu");
        return mv;
    }

    //添加学生信息服务
    @PostMapping(value = "/addStudent", consumes = "application/x-www-form-urlencoded")
    @ResponseBody
    public ModelAndView addStudent(@RequestParam Map<String, String> requestParams) {
            Student student = new Student();
            student.setNumber(Integer.valueOf(requestParams.get("number")));
            student.setName(requestParams.get("name"));
            student.setGender(requestParams.get("gender"));
            student.setGrade(requestParams.get("grade"));
            student.setClbum(requestParams.get("clbum"));
            student.setAge(Integer.parseInt(requestParams.get("age")));
            service.AddStudent(student);
            return new ModelAndView("redirect:/student/StudentAll");
    }

    @PostMapping(value = "/UpdateStudentServlet", consumes = "application/x-www-form-urlencoded")
    @ResponseBody
    public ModelAndView UpdateStudent(@RequestParam Map<String, String> requestParams) {
        Student student = new Student();
        student.setNumber(Integer.valueOf(requestParams.get("number")));
        student.setName(requestParams.get("name"));
        student.setGender(requestParams.get("gender"));
        student.setGrade(requestParams.get("grade"));
        student.setClbum(requestParams.get("clbum"));
        student.setAge(Integer.parseInt(requestParams.get("age")));
        service.UpdateStudent(student);
        return new ModelAndView("redirect:/student/StudentAll");
    }
}
